package com.study.mvc.utils;

import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import com.study.mvc.annotation.Controller;
import com.study.mvc.annotation.FormBean;
import com.study.mvc.annotation.Forward;
import com.study.mvc.annotation.RequestMapping;


public class Struts_config_anotation {

	public Struts_config_anotation() {
		// TODO Auto-generated constructor stub
	}

	public static Map<String,XmlBean> loadStrutsXml(String xmlPath) throws Exception{
		
		SAXBuilder builder =new SAXBuilder();
		Document document=builder.build(new File(xmlPath));
		Element root=document.getRootElement();
		Map<String,XmlBean> retMap=new HashMap<String,XmlBean>();
	/*	Element actionForm=root.getChild("formbeans");
		Element actionroot=root.getChild("action-mapping");*/
		Element actionscan=root.getChild("action-scan");
		
		String classpath = Struts_config_anotation.class.getClassLoader().getResource("").getPath();
		
	/*	List<Element> actions=actionroot.getChildren();		
		for(Element e:actions){
			XmlBean xmlBean=new XmlBean();
			String name=e.getAttributeValue("name");
			xmlBean.setBeanName(name);
			List<Element> forms=actionForm.getChildren();
			for(Element fm:forms){
				if(name.equals(fm.getAttributeValue("name"))){				
					xmlBean.setFormClass(fm.getAttributeValue("class"));
					break;
				}
			}
			String path=e.getAttributeValue("path");
			xmlBean.setPath(path);
			
			String type=e.getAttributeValue("type");
			xmlBean.setActionType(type);
			
			List<Element> frowards=e.getChildren();
			Map<String,String> forwardMap=new HashMap<String,String>();
			for(Element f:frowards){
				
				String fname=f.getAttributeValue("name");
				String fvalue=f.getAttributeValue("value");
				forwardMap.put(fname, fvalue);		
			}
			xmlBean.setActionForward(forwardMap);
			
			retMap.put(path, xmlBean);
			
		 }*/
		
		
		List<Element> actionscanItems=actionscan.getChildren();
		List<Class> clsList=new ArrayList<Class>();
		for(Element e:actionscanItems){
			
			String packageName=e.getAttributeValue("package");
			File file=new File(classpath+packageName.replace(".","/"));
			File[] files=file.listFiles();		
			for(File f:files){
				if(!f.isDirectory()){
					try {
						String fileName=f.getName().substring(0,f.getName().lastIndexOf("."));
						clsList.add(Class.forName(packageName+"."+fileName));
					} catch (Exception e2) {
						 e2.printStackTrace();
					}					
				}
			}
			
		}
		
		for(Class c:clsList){
			
			Controller controller=(Controller)c.getAnnotation(Controller.class);
			if(controller!=null){
				XmlBean xmlBean=new XmlBean();
				
				RequestMapping requestMaping=(RequestMapping)c.getAnnotation(RequestMapping.class);
				xmlBean.setPath(requestMaping.value());
				xmlBean.setActionType(c.getName());
				
				
				Forward forward=(Forward)c.getAnnotation(Forward.class);
				Map<String,String> forwardMap=new HashMap<String,String>();
				
				forwardMap.put("chenggong", forward.chenggong());
				forwardMap.put("shibai",forward.shibai());				
				xmlBean.setActionForward(forwardMap);
				
				FormBean formBean=(FormBean)c.getAnnotation(FormBean.class);
				xmlBean.setFormClass(formBean.clasz());
				xmlBean.setBeanName(formBean.beanName());
				retMap.put(xmlBean.getPath(), xmlBean);
				
			}
			
		}
		
		
		return retMap;
		
		
	}
}
